package com.atjinggu.jinggumail.order.listener;

import com.atjinggu.jinggumail.order.entity.OrderEntity;
import com.atjinggu.jinggumail.order.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author jingming
 * @Type OrderCloseListener.java
 * @Desc
 * @date 2021/3/17 12:11
 */
@RabbitListener(queues = "order.release.order.queue")
@Component
public class OrderCloseListener {
    @Autowired
    OrderService orderService;

    @RabbitHandler
    public void orderClose(OrderEntity entity, Message message, Channel channel) throws IOException {
        System.out.println("订单到期----开始失效订单");
        try {
            orderService.closeOrder(entity);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e){
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}